/*
 * redmicro all Copyright (c)
 */

/**
 * Created by seeker910 on 2014/12/31.
 */
Rsd.define('Rsd.Faith.controls.XiaoQuTreeView', {
    extend: 'Rsd.control.Tree',
    requires: [
        'Rsd.control.Text',
        'Rsd.control.Grid',
        'Rsd.control.ToolBar'
    ],
    xtype: 'tree-xiaoqu',
    toolBar:{
        xtype:'toolBar',
        width:'100%',
        height:'100%',
        layout:'hbox',
        margin:'5 0 5 0',
        style:{lineHeight:'normal'},
        items:[
            {
                xtype:'text',
                label:'小区',
                width:230,
                margin:'0 0 0 10'
            },
            {
                xtype:'link',
                style:{fontWeight:'normal'},
                text:'[查找]',
                margin:'0 5 15 0',
                width:50,
                align:'right',
                handler:'btn_find'
            }
        ]
    },
    showLine:false,
    showRoot:false,
    margin:'0 2 0 0',
    width:300,
    /*
     * */
    constructor: function (config) {
        config = config || {};
        Rsd.apply(this, config);


    },
    btn_find:function btn_find()
    {
        var ming_cheng = this.label.content.items[0].getText();
        this.loadData({});
    },
    /*
    *
    * */
    loadData:function loadData(args)
    {
        var _args = args||{};
        //debugger;
        var me = this;
        me.showLoading();
        var _tree =  this;
        var _store = Rsd.create('Rsd.Faith.stores.XiaoQuStore',{});
        _store.load(_args,function (data) {

            me.addNodes(_tree.root.id, data.data);
            _tree.build();

            me.closeLoading();
        });
    },

    /*
     * */
    addNodes: function (pid, menus) {
        var _menu;
        var _node;
        var me = this;
        var _tree = this
        var _list = menus;
        for (var i in _list) {
            _menu = _list[i];

            _node = {id: pid + '__' + i,text:_menu.MingCheng, pid: pid, tag: _menu}

            if (!_menu.children
                || (Rsd.isArray(_menu.children) && _menu.children.length == 0)) {
                _node.dblClick = function () {

                   me.dataBind(this.tag);
                }
            }
            Rsd.apply(_node, _menu)

            _tree.addTreeNode(_node);

            delete  _node.children;
            if (_menu.children) {
                me.addNodes(_node.id, _menu.children);
            }
        }
    }
});

